import { request } from "../utils";
import config from "../config";
import axios from "axios";

const { SERVER_URL } = config;
export default {
  getApps,

  getCategories,
  createCategory,
  updateCategory,

  getShowcases,
  getShowcase,
  createShowcase,
  updateShowcase,
  deleteShowcase,

  getPromos,
  getPromo,
  createPromo,
  updatePromo,
  deletePromo,

  getProducts,
  createProduct,
  updateProduct,

  getUsers,
  getUser,
  updateUser,

  draggableUpload,
  upload,
};

async function getApps() {
  const { data } = await request({
    url: SERVER_URL + "/open/apps",
    method: "GET",
  });
  return data;
}

async function getCategories() {
  const { data } = await request({
    url: SERVER_URL + "/categories",
    method: "GET",
  });
  return data;
}

async function createCategory({
  name,
  icon_url,
  priority,
  is_required,
  is_multiple,
}) {
  const res = await request({
    url: SERVER_URL + "/categories",
    method: "POST",
    data: {
      name,
      icon_url,
      priority,
      is_required,
      is_multiple,
    },
  });
  return res;
}

async function updateCategory({
  category_id,
  name,
  icon_url,
  priority,
  is_required,
  is_multiple,
}) {
  const res = await request({
    url: SERVER_URL + "/categories/" + category_id,
    method: "PUT",
    data: {
      name,
      icon_url,
      priority,
      is_required,
      is_multiple,
    },
  });
  return res;
}

async function getPromos({ keywords, scene, page, page_size }) {
  const { data } = await request({
    url: SERVER_URL + "/promos",
    method: "GET",
    params: {
      scene,
      keywords,
      page,
      page_size,
    },
  });
  return data;
}

async function getPromo({ promo_id }) {
  const { data } = await request({
    url: SERVER_URL + "/promos/" + promo_id,
    method: "GET",
  });
  return data;
}

async function createPromo({ title, media_url, scene, mp_path, priority }) {
  const { data } = await request({
    url: SERVER_URL + "/promos",
    method: "POST",
    data: {
      title,
      media_url,
      scene,
      mp_path,
      priority,
    },
  });
  return data;
}

async function updatePromo({
  promo_id,
  title,
  media_url,
  scene,
  mp_path,
  priority,
}) {
  const res = await request({
    url: SERVER_URL + "/promos/" + promo_id,
    method: "PUT",
    data: {
      title,
      media_url,
      scene,
      mp_path,
      priority,
    },
  });
  return res;
}

async function deletePromo({ promo_id }) {
  const { data } = await request({
    url: SERVER_URL + "/promos/" + promo_id,
    method: "DELETE",
  });
  return data;
}

async function getShowcases({ with_info, page, page_size }) {
  const { data } = await request({
    url: SERVER_URL + "/showcases",
    method: "GET",
    params: {
      with_info,
      page,
      page_size,
    },
  });
  return data;
}

async function getShowcase({ showcase_id }) {
  const { data } = await request({
    url: SERVER_URL + "/showcases/" + showcase_id,
    method: "GET",
  });
  return data;
}

async function createShowcase({
  name,
  description,
  media_type,
  media_urls,
  product_id,
}) {
  const res = await request({
    url: SERVER_URL + "/showcases",
    method: "POST",
    data: {
      name,
      description,
      media_type,
      media_urls,
      product_id,
    },
  });
  return res;
}

async function updateShowcase({
  showcase_id,
  name,
  description,
  media_urls,
  media_type,
  product_id,
}) {
  const res = await request({
    url: SERVER_URL + "/showcases/" + showcase_id,
    method: "PUT",
    data: {
      name,
      description,
      media_urls,
      media_type,
      product_id,
    },
  });
  return res;
}

async function deleteShowcase({ showcase_id }) {
  const { data } = await request({
    url: SERVER_URL + "/showcases/" + showcase_id,
    method: "DELETE",
  });
  return data;
}

async function getProducts({ keywords, category_id, page, page_size }) {
  const { data } = await request({
    url: SERVER_URL + "/products",
    method: "GET",
    params: {
      keywords,
      category_id,
      page,
      page_size,
    },
  });
  return data;
}

async function createProduct({
  name,
  category_id,
  price,
  remark,
  thumbnail_url,
  priority,
}) {
  const res = await request({
    url: SERVER_URL + "/products",
    method: "POST",
    data: {
      name,
      category_id,
      priority,
      price,
      remark,
      thumbnail_url,
    },
  });
  return res;
}

async function updateProduct({
  product_id,
  name,
  category_id,
  priority,
  price,
  remark,
  thumbnail_url,
}) {
  const res = await request({
    url: SERVER_URL + "/products/" + product_id,
    method: "PUT",
    data: {
      name,
      category_id,
      price,
      remark,
      thumbnail_url,
      priority,
    },
  });
  return res;
}

async function draggableUpload(option) {
  const file = option.get("file");
  return upload(
    {
      file,
    },
    "models"
  );
}

async function upload(option, folder_name) {
  const { data: credentials } = await request({
    url: SERVER_URL + "/get-upload-token",
    method: "POST",
    data: {
      count: 1,
      folder_name,
    },
  });

  const file = option.file;
  const form = new FormData();

  form.append("key", credentials.key);
  form.append("policy", credentials.policy);
  form.append("OSSAccessKeyId", credentials.OSSAccessKeyId);
  form.append("signature", credentials.signature);
  form.append("x-oss-security-token", credentials["x-oss-security-token"]); // 使用STS签名时必传。);

  // 一定是最后一项
  form.append("file", file);

  await axios({
    url: credentials.domain,
    method: "POST",
    headers: {
      "Content-Type": "multipart/form-data",
    },
    data: form,
  });

  const url = credentials.domain + credentials.key;
  return url;
}

async function getUsers({ page, page_size, keywords }) {
  const { data } = await request({
    url: SERVER_URL + "/users",
    method: "GET",
    params: {
      page,
      page_size,
      keywords,
    },
  });
  return data;
}

async function getUser({ user_id }) {
  const { data } = await request({
    url: SERVER_URL + "/users/" + user_id,
    method: "GET",
  });
  return data;
}

async function updateUser({ user_id, name, address, phone }) {
  const { data } = await request({
    url: SERVER_URL + "/users/" + user_id,
    method: "PUT",
    data: {
      name,
      phone,
      address,
    },
  });
  return data;
}
